$(function () {
    var login = {
        data: {

            name: '',
            ename: ''
        },
        init: function init() {
            this.goLogin();
        },
        goLogin: function goLogin() {
            var that = this;
            $('#confirm').click(function () {
                that.data.name = $.trim($('#name').val()).toLowerCase();
                that.data.ename = $.trim($('#ename').val()).toLowerCase();

                // 判断是否存在用户
                if (that.validate()) {
                    fetch("./lib/attendance.json").then(function (res) {
                        return res.json();
                    }).then(function (data) {
                        var totalData = data["RECORDS"];
                        var item = totalData.find(function (item) {
                            return item.nm === that.data.name && item.nm_en.toLowerCase() === that.data.ename;
                        });
                        if (!item) {
                            alert('Opps，小易还不认识这位小可爱 查看最新岗位，加入我们吧！');
                        } else {
                            localStorage.setItem('userInfo', JSON.stringify({ record: item }));
                            window.location.href = "detail.html";
                        }
                    });
                } else {
                    that.onInput();
                }
            });
        },
        validate: function validate() {
            var result = true;
            this.data.name = $.trim($('#name').val()).toLowerCase();
            this.data.ename = $.trim($('#ename').val()).toLowerCase();
            if (!this.data.name) {
                $('.name-error').show();
                result = false;
            } else {
                $('.name-error').hide();
            }

            if (!this.data.ename) {
                $('.ename-error').show();
                result = false;
            } else {
                $('.ename-error').hide();
            }
            return result;
        },
        onInput: function onInput() {
            var that = this;
            $('#name,#ename').off('input').on('input', function () {
                that.validate();
            });
        }
    };
    login.init();
});